﻿using System;
using System.Text;
using System.Collections.Generic;
using SdmxMl.Common;
using SdmxMl.Structure;
using SdmxMl.Manager;
using SdmxMl.GenericMetadata;

namespace SilcQuest
{
    public class CatiSuiviItem
    {
        #region Internal Members

        private SmManager sm;
        private AttributeValueSetType attValSet;
        private ReportStructureType report;
        #endregion

        #region Construction

        public CatiSuiviItem(SmManager smMgr, ReportStructureType r, AttributeValueSetType at)
        {
            sm = smMgr;
            attValSet = at;
            report = r;
        }

        #endregion

        #region Properties

        public string Id
        {
            get { return attValSet.TargetValuesTypeList.GetComponentValue("ID").SimpleValue; }
        }

        public string Date
        {
            get { return attValSet.TargetValuesTypeList.GetComponentValue("DT").SimpleValue; }
        }
        public string VarName
        {
            get { return attValSet.TargetValuesTypeList.GetComponentValue("VARNAME").SimpleValue; }
        }

        public string Problem
        {
            get { return GetTextOfCodedValue("PROBLEM"); }
            set { SetValOfAttribute("PROBLEM", value); }
        }

        public string User
        {
            get { return GetTextOfCodedValue("USER"); }
        }

        public string Lang
        {
            get { return GetTextOfCodedValue("LANG"); }
        }

        public string FieldType
        {
            get { return GetTextOfCodedValue("FIELD_TYPE"); }
        }
        
        public string TranslationStatus
        {
            get { return GetTextOfCodedValue("TR_STAT"); }
        }

        public string UserVal
        {
            get { return GetValOfAttribute("USER"); }
            set { SetValOfAttribute("USER", value); }
        }

        public string LangVal
        {
            get { return GetValOfAttribute("LANG"); }
            set { SetValOfAttribute("LANG", value); }
        }

        public string FieldTypeVal
        {
            get { return GetValOfAttribute("FIELD_TYPE"); }
            set { SetValOfAttribute("FIELD_TYPE", value); }
        }

        public string TranslationStatusVal
        {
            get { return GetValOfAttribute("TR_STAT"); }
            set { SetValOfAttribute("TR_STAT", value); }
        }

        #endregion

        private string GetValOfAttribute(string fieldId)
        {
            ReportedAttributeType r = attValSet.ReportedAttributeList.GetReportedAttributeOfConcept(fieldId);
            if (r == null)
                throw new ApplicationException("Unknown field: " + fieldId);

            return r.Value.ToString();
        }

        private void SetValOfAttribute(string fieldId, string val)
        {
            ReportedAttributeType r = attValSet.ReportedAttributeList.GetReportedAttributeOfConcept(fieldId);
            if (r == null)
                throw new ApplicationException("Unknown field: " + fieldId);

            r.Value = new MultiLang(val);
        }

        public CodeListType GetCodelistOfField(string fieldId)
        {
            CodeListType clt = null;
             MetadataAttributeType meta = report.MetadataAttributeList.GetAttributeReferingConcept(fieldId);
            if (meta == null)
                throw new ApplicationException("Unknown field in report definition: " + fieldId);
            if (meta.RepresentationInfo.IsFilled)
            {
                clt = sm.GetArtefact(SdmxArtefactType.CodeLists, meta.RepresentationInfo, true) as CodeListType;
                if (clt == null)
                    throw new ApplicationException("Unable to fetch refered codelist:  " + meta.RepresentationInfo.ToString());
            }
            return clt;
       }

        private string GetTextOfCodedValue(string fieldId)
        {
            string txt = string.Empty;

            string val = GetValOfAttribute(fieldId);
            CodeListType clt = GetCodelistOfField(fieldId);
            if (clt != null)
            {
                ISdmxHierarchyListItem item = clt.CodeList.LocateItem(val);
                txt = item == null ? val : item.Name.ToString();
            }
            else
                txt = val;

            return txt;

        }
    }
}
